<?php
/**
 * CreateTime: 2022/6/11 15:20
 */

namespace App\ServiceChecker\OperateChecker;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Models\Company\CompanyModel;
use App\Models\Department\DepartmentModel;
use App\Models\NewContract\ContractModel;
use App\Models\Position\PositionModel;
use App\Models\Staff\StaffDepartmentModel;
use App\Models\Staff\StaffModel;
use App\ServiceChecker\BasicServiceChecker;
use Faker\Provider\Company;
use Illuminate\Http\Exceptions\HttpResponseException;

class DeleteOperateChecker extends BasicServiceChecker
{
    // 删除公司
    const DELETE_COMPANY = 1;
    // 删除部门
    const DELETE_DEPT = 2;
    // 删除用户
    const DELETE_STAFF = 3;
    // 删除职位
    const DELETE_POSITION = 4;
    // 删除用户
    const DELETE_AUTH = 5;
    // 删除角色
    const DELETE_ROLE = 6;
    // 删除信用项目
    const DELETE_CREDIT_PROJECT = 7;
    // 删除合同
    const DELETE_CONTRACT = 8;

    public function deleteOperateCheckerByDeleteType($sourceId,$deleteType){
        switch($deleteType){
            case self::DELETE_COMPANY:
                $this->checkerCompanyDeleteByCompanyId($sourceId);
                break;
            case self::DELETE_DEPT:
                $this->checkerDepartmentByDeptId($sourceId);
                break;
            case self::DELETE_STAFF:
                $this->checkerStaffByStaffId($sourceId);
                break;
            case self::DELETE_POSITION:
                break;
            case self::DELETE_AUTH:
                break;
            case self::DELETE_ROLE:
                break;
            case self::DELETE_CREDIT_PROJECT:
                break;
            case self::DELETE_CONTRACT:
                break;
            default:
                break;
        }
    }

    private function checkerCompanyDeleteByCompanyId($companyId){
        $companyModel = new CompanyModel();
        $departmentModel = new DepartmentModel();
        $staffModel = new StaffModel();
        $positionModel = new PositionModel();
        $contractModel = new ContractModel();

        // 校验当前公司下是否存在子公司
        $hasChildCompany = $companyModel->isExistByCondition(
            [
                'company_parent_id' => $companyId,
                'status' => BaseConsts::STATUS_NORMAL
            ]
        );
        if($hasChildCompany){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "存在下级公司不允许删除",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }

        // 校验当前公司下是否存在部门
        $hasDepartment = $departmentModel->isExistByCondition([
            'company_id' => $companyId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($hasDepartment){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "存在部门不允许删除",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }

        // 校验当前公司下是否存在用户
        $hasStaff = $staffModel->isExistByCondition([
            'company_id' => $companyId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($hasStaff){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "存在用户不允许删除",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }

        // 校验公司下是否存在职位
        $hasPosition = $positionModel->isExistByCondition([
            'company_id' => $companyId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($hasPosition){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "存在职位不允许删除",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }

        // 校验公司下是否存在合同
        $hasContract = $contractModel->isExistByCondition([
            'company_id' => $companyId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($hasContract){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "存在职位不允许删除",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }
    }

    private function checkerDepartmentByDeptId($departmentId){
        $departmentModel = new DepartmentModel();
        $staffDepartmentModel = new StaffDepartmentModel();
        $positionModel = new PositionModel();
        $contractModel = new ContractModel();
        // 校验当前部门下是否存在子部门
        $hasDept = $departmentModel->isExistByCondition([
           'department_parent_id' => $departmentId,
           'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($hasDept){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "存在部门不允许删除",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }

        // 校验当前部门下是否存在用户
        $hasStaff = $staffDepartmentModel->isExistByCondition([
            'department_id' => $departmentId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($hasStaff){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "存在用户不允许删除",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }

        // 校验部门下是否存在职位
        $hasPosition = $positionModel->isExistByCondition([
            'department_id' => $departmentId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($hasPosition){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "存在职位不允许删除",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }

        // 校验部门下是否存在合同
        $hasContract = $contractModel->isExistByCondition([
            'responsible_department_id' => $departmentId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($hasContract){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "存在合同不允许删除",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }
    }

    private function checkerStaffByStaffId($staffId){
        $contractModel = new ContractModel();

        // 校验角色下是否存在合同
        $hasContract = $contractModel->isExistByCondition([
            'responsible_staff_id' => $staffId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($hasContract){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "存在合同不允许删除",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }
    }

    private function deleteTable(){

    }
}